package com.example.myandroidactualcombat.ui.home;

import android.util.Log;
import android.widget.ArrayAdapter;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.Adapter.CollectionAdapter;
import com.example.myandroidactualcombat.ChooseActivity;
import com.example.myandroidactualcombat.data.Book;
import com.example.myandroidactualcombat.data.ChooseBook;
import com.example.myandroidactualcombat.data.Collection;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class HomeViewModel extends ViewModel {
    List<Book>bookList;
    int userid;
    List<Collection>collectionList;
    private MutableLiveData<List<Collection>>mCollections=new MutableLiveData<>();

    public HomeViewModel() {
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }
    public LiveData<List<Collection>>  getData(){
        getCollection();
        mCollections.setValue(collectionList);
        return mCollections;
    }

    private void getCollection() {
        bookList= LitePal.findAll(Book.class);
        collectionList=new ArrayList<>();
        List<ChooseBook>chooseBookList=LitePal.where("userid=?",""+userid).find(ChooseBook.class);
        ArrayList<Integer>ids=new ArrayList<>();
        if (chooseBookList.size()!=0){
            for (int i=0;i<chooseBookList.size();i++){
                ids.add(chooseBookList.get(i).getBookid());
            }
        }
        for (int j=0;j<bookList.size();j++){
            Book book = bookList.get(j);
            Collection item=new Collection(userid,book.getId(),book.getBookname(),book.getImageid(),false);
            if (ids.contains(book.getId())){
                item.setIfcollection(true);
            }
            collectionList.add(item);
        }
    }
}